package com.os.sys.controller;

import com.github.pagehelper.PageInfo;
import com.os.sys.common.utility.PubClass;
import com.os.sys.common.utility.StringClass;
import com.os.sys.common.utility.XJsonInfo;
import com.os.sys.mapper.RoleMapper;
import com.os.sys.model.ILoginUser;
import com.os.sys.model.T_SYS_Role;
import com.os.sys.model.T_SYS_User;
import com.os.sys.repository.RoleRepository;
import com.os.sys.repository.Role_To_UserRepository;
import com.os.sys.service.RoleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

@RestController
@RequestMapping("/api/sys/role")
public class RoleController {
    @Resource
    private RoleRepository roleRepository;
    @Resource
    private Role_To_UserRepository role_to_userRepository;
    @Resource
    private RoleService rolerService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PubClass pubClass;

    @RequestMapping("/getLoginUserPower")
    public XJsonInfo getLoginUserPower() {
        ILoginUser loginUser = pubClass.getLoginUser();
        if (loginUser == null) {
            return new XJsonInfo(false, "无法获得登录用户信息");
        }
        List<String> list = rolerService.getRolesPowerListByUserId(loginUser.getLoginUserID());
        return new XJsonInfo().setData(list);
    }

    @RequestMapping("/getList")
    public XJsonInfo getList(T_SYS_Role info) {
        Pageable pageable = pubClass.createPagenation();
        Page page = roleRepository.findAll((Specification<T_SYS_Role>) (root, query, cb) -> {

            if (StringClass.isNotBlank(info.getRoleKey())) {
                query.where(cb.equal(root.<String>get("roleKey"), info.getRoleKey()));
            }

            if (StringClass.isNotBlank(info.getRoleKey())) {
                query.where(cb.equal(root.<String>get("name"), info.getName()));
            }

            return null;
        }, pageable);

        return new XJsonInfo().setPageData(page);
    }

    @RequestMapping(value = "/getModel")
    public XJsonInfo getModel(Long id) {
        T_SYS_Role info;
        if (id == 0 || id == null) {
            return new XJsonInfo(false);
        }
        info = roleRepository.findById(id).orElse(null);
        return new XJsonInfo().setData(info);
    }

    // 根据角色id，获得拥有这个角色的所有用户集合
    @RequestMapping("/getListForUserRoleByRoleId")
    public XJsonInfo getListForUserRoleByRoleId(T_SYS_User info, Long roleId) {
        pubClass.createPagenationForMybaits();
        List<T_SYS_User> list = roleMapper.findUsersByRoleId(info, roleId);
        PageInfo page = new PageInfo(list);
        return XJsonInfo.success().setPageData(page);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public XJsonInfo save(@RequestBody T_SYS_Role info) {
        roleRepository.save(info);

        return new XJsonInfo();
    }

    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public XJsonInfo remove(String ids) {
        List<Long> arrIds= StringClass.toLongList(ids);
        roleRepository.deleteAllByIdIn(arrIds);
        pubClass.addLog("T_OS_Role", "删除系统角色:" +ids);

        return new XJsonInfo();
    }

    // 根据角色Id和用户Id，删除用户角色记录
    @RequestMapping(value = "/removeUserRole", method = RequestMethod.POST)
    public XJsonInfo removeUserRole(Long userId, Long roleId) {
        role_to_userRepository.deleteByUserIdAndRoleId(userId, roleId);
        return new XJsonInfo();
    }
}
